package com.tledu.spring_mybatis.service.impl;

import com.tledu.spring_mybatis.mapper.UserMapper;
import com.tledu.spring_mybatis.model.User;
import com.tledu.spring_mybatis.service.IUserService;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author cyrus
 */
@Service
@AllArgsConstructor
public class UserServiceImpl implements IUserService {

    private final UserMapper userMapper;

    @Override
    public User selectById(int id) {
        return userMapper.selectById(id);
    }
    @Override
    // 加上注解之后代表下面的方法要开启事务
    @Transactional(rollbackFor = Exception.class,isolation = Isolation.SERIALIZABLE)
    public int insert(User user) {
        int i = userMapper.insert(user);
        // 只是为了演示
        int t = 10 / 0;
        User updateUser = new User();
        updateUser.setId(2);
        updateUser.setUsername(user.getUsername());
        userMapper.update(updateUser);
        return i;
    }
}
